package edu.mis.model;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import edu.mis.model.base.BaseOrderList;

import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
@SuppressWarnings("serial")
public class OrderList extends BaseOrderList<OrderList> {


	public static final OrderList dao = new OrderList().dao();
    private User user = new User();
	private Collection<OrderDetail> orderDetails = new HashSet<>(0);
	private Collection<OrderDetailCustomer> orderDetailCustomers = new HashSet<>(0);
	private Collection<OrderDetailRoom> orderDetailRooms = new HashSet<>(0);

	public Collection<OrderDetailRoom> getOrderDetailRooms() {
		return (orderDetailRooms == null)?OrderDetailRoom.dao.findAllByOrderlistId(getId()):orderDetailRooms;
	}

	public void setOrderDetailRooms(Collection<OrderDetailRoom> orderDetailRooms) {
		this.orderDetailRooms = orderDetailRooms;
	}



	/**
	 * 表关联查询_得到订单的类型
	 * 类型:订单为1:N关系，得到订单对象的类型。
	 * @return
	 */
//	public Seat findSeatBySeatId(){
//		return Seat.dao.findById(get("statusId"));
//	}


	public User getUser() {
		return (user == null)?User.dao.findById(getUserId()):user;
	}

	public void setUser(User user) {
		this.setUserId(user.getId());
		this.user = user;
	}

//	public Seat getSeat() {
//		return seat;
//	}
//
//	public void setSeat(Seat seat) {
//		this.setSeatId(seat.getId());
//		this.seat = seat;
//	}

	public Collection<OrderDetail> getOrderDetails() {
		return (orderDetails == null)?OrderDetail.dao.findAllByOrderListId(getId()):orderDetails;
	}

	public void setOrderDetails(Collection<OrderDetail> orderDetails) {
		this.orderDetails = orderDetails;
	}

    public Collection<OrderDetailCustomer> getOrderDetailCustomers() {
        return (orderDetailCustomers == null)?OrderDetailCustomer.dao.findAllByOrderListId(getId()):orderDetailCustomers;
    }

    public void setOrderDetailCustomers(Collection<OrderDetailCustomer> orderDetailCustomers) {
        this.orderDetailCustomers = orderDetailCustomers;
    }


    private OrderStatus orderStatus = new OrderStatus();

    public OrderStatus getOrderStatus() {
        return (orderStatus == null)?OrderStatus.dao.findById(getStatusId()):orderStatus;
    }

    public void setOrderStatus(OrderStatus orderStatus) {
        this.setStatusId(orderStatus.getId());
        this.orderStatus = orderStatus;
    }

    /**
     * 表关联查询得到菜品的菜品号
     * 类型：订单为1：N关系，得到菜品对象的类型
     * @return
     */
    public List<Record> findAllOrderDetailsWithRoomType(int id){
        return Db.find("SELECT d.*,t.typeName,t.price FROM(SELECT * FROM order_detail  WHERE orderListId=?) d LEFT JOIN room_type t on d.roomTypeId=t.id",id);
    }
    public List<Record> findAllOrderDetailsWithCustomer(int id){
        return Db.find("SELECT d.*,c.customerName,c.sex,c.phone,c.IDCard FROM(SELECT * FROM order_detail_customer  WHERE orderListId=?) d LEFT JOIN customer c on d.customerId=c.id",id);
    }
    public List<Record> findAllOrderDetailsWithRoom(int id){
        return Db.find("SELECT d.*,r.roomNumber,t.typeName,t.price,s.statusName FROM(SELECT * FROM order_detail_room  WHERE orderListId=?) d  LEFT JOIN room r ON d.roomId=r.id LEFT JOIN room_type t ON r.roomTypeId=t.id LEFT JOIN room_status s ON r.roomStatusId=s.id",id);
    }
    public List<Record> findAllOrderListWithStatusId(Integer statusId){
        return Db.find("SELECT l.*,s.statusName FROM(SELECT * FROM order_list  WHERE statusId=?) l LEFT JOIN order_status s on l.statusId=s.id",statusId);
    }
    public List<Record> findAllOrderListWithCheckin(String checkin){
        return Db.find("SELECT l.*,s.statusName FROM(SELECT * FROM order_list  WHERE checkin=?) l LEFT JOIN order_status s on l.statusId=s.id",checkin);
    }
    public List<Record> findCheckinOrderListWithCheckinAndStatus(String checkin, Integer statusId){
        return Db.find("SELECT l.*,s.statusName FROM(SELECT * FROM order_list  WHERE checkin=? and statusId=?) l LEFT JOIN order_status s on l.statusId=s.id",checkin, statusId);
    }
    public List<Record> findAllOrderListWithCheckout(String checkout){
        return Db.find("SELECT l.*,s.statusName FROM(SELECT * FROM order_list  WHERE checkout=?) l LEFT JOIN order_status s on l.statusId=s.id",checkout);
    }
    public List<Record> findCheckoutOrderListWithCheckoutAndStatus(String checkout, Integer statusId){
        return Db.find("SELECT l.*,s.statusName FROM(SELECT * FROM order_list  WHERE newCheckout=? and statusId=?) l LEFT JOIN order_status s on l.statusId=s.id", checkout, statusId);
    }
    /**
     * 关联查询_查询所有订单及其类型(类型:订单为1对多)
     * @return
     */
    public List<Record> findAllRecord(){
        return Db.find("SELECT l.*,s.statusName FROM order_list l LEFT JOIN order_status s ON l.statusId = s.id" );
    }
    /**
     * 查询所有订单
     * @return
     */
    public List<OrderList> findAll(){
        return dao.find("SELECT * FROM order_list");
    }
    public List<OrderDetailRoom> findAllRooms(int id){
        return OrderDetailRoom.dao.find("SELECT * FROM order_detail_room WHERE orderListId=?",id);
    }
    /**
     * 根据订单名查找订单
     * @param orderName
     * @return
     */
    public OrderList findOrderlistByOrderName(String orderName){
        return findFirst("SELECT * FROM order_list WHERE orderName = ? ",orderName);
    }
    public OrderList findOrderListByDateTime(Date orderTime) {
        return findFirst("SELECT * FROM order_list WHERE orderTime = ? ",orderTime);
    }


    public User findUserByUserId(){
        return User.dao.findById(get("userId"));
    }
}
